/*
	Visp is an opensource visual performance application built with Adobe® AIR™.
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Mike Creighton can be found at http://www.mikecreighton.com	
*/
package com.visp.transitions
{
	import com.visp.events.TransitionEvent;
	
	import flash.events.EventDispatcher;
	
	[Event(name="removedTransition", type="com.visp.events.TransitionEvent")]
	[Event(name="transitionStart", type="com.visp.events.TransitionEvent")]
	[Event(name="transitionExpired", type="com.visp.events.TransitionEvent")]
	[Event(name="newTransition", type="com.visp.events.TransitionEvent")]
	
	public class TransitionManager extends EventDispatcher
	{
		private static var _instance : TransitionManager;
		
		public static const MIN_DURATION : int = 1000;
		public static const MAX_DURATION : int = 300000;
		
		private var _transition : ITransition;
		private var _duration : Number
		
		public static function getInstance() : TransitionManager
		{
			if(TransitionManager._instance == null)
				TransitionManager._instance = new TransitionManager();
			return TransitionManager._instance;
		}
		
		public function TransitionManager()
		{
			super(null);
			this._duration = 2000;
		}
		
		public function initialize() : void {
			
		}
		
		/**
		 * Sets the duration for the transition in milliseconds.
		 */
		public function setDuration(time : Number) : void 
		{
			this._duration = time;
		}
		
		/**
		 * Gets the duration to be used for the current
		 * transition in milliseconds.
		 */
		public function getDuration() : Number 
		{
			return this._duration;
		}
		
		public function setTransition(transition : ITransition) : void
		{
			this._transition = transition;
			this._transition.addEventListener(TransitionEvent.START, _handleTransitionStarted);
			this._transition.addEventListener(TransitionEvent.EXPIRED, _handleTransitionExpired);
			this.dispatchEvent(new TransitionEvent(TransitionEvent.NEW));
		}
		
		private function _handleTransitionStarted(e : TransitionEvent) : void 
		{
			this.dispatchEvent(new TransitionEvent(TransitionEvent.START));
		}
		
		private function _handleTransitionExpired(e : TransitionEvent) : void
		{
			this.dispatchEvent(new TransitionEvent(TransitionEvent.EXPIRED));
		}
		
		public function removeTransition() : void
		{
			this._transition.removeEventListener(TransitionEvent.START, _handleTransitionStarted);
			this._transition.removeEventListener(TransitionEvent.EXPIRED, _handleTransitionExpired);
			this._transition.destroy();
			this._transition = null;
			this.dispatchEvent(new TransitionEvent(TransitionEvent.REMOVED));
		}
		
		/**
		 * Used for checking whether or not there is a currently assigned transition
		 */
		public function get hasTransition() : Boolean
		{
			return this._transition != null;
		}
		
		public function getTransition() : ITransition
		{
			return this._transition;
		}
		
	}
}